package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.db.cache.YhCache;
import com.sandking.db.cache.LtCache;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import com.sandking.db.cache.FwqCache;
import com.sandking.db.cache.LmsjCache;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import com.sandking.db.cache.LmCache;
import java.io.ByteArrayOutputStream;

/**
 * 联盟
 */
public class Lm {

	public static final String TABLENAME = "联盟";
	public static final String CLASSNAME = "Lm"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** tempid */
	private int tempid;
	
	/** 名称 */
	private String mc;
	
	/** 创建人id */
	private int cjrid;
	
	/** 服务器_id */
	private int fwq_id;
	
	/** 战斗力 */
	private int zdl;
	
	/** 图标 */
	private String tb;
	
	/** 创建时间 */
	private java.util.Date cjsj;
	
	/** 人数 */
	private int rs;
	
	
	public Lm() {
		super();
	}
	
	public Lm(int id, int tempid, String mc, int cjrid, int fwq_id, int zdl, String tb, java.util.Date cjsj, int rs) {
		super();
		this.id = id;
		this.tempid = tempid;
		this.mc = mc;
		this.cjrid = cjrid;
		this.fwq_id = fwq_id;
		this.zdl = zdl;
		this.tb = tb;
		this.cjsj = cjsj;
		this.rs = rs;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getTempid() {
		return tempid;
	}
	
	public void setTempid(int tempid) {
		this.tempid = tempid;
		addUpdateColumn("tempid",tempid);
	}
	
	public void changeTempidWith(int tempid){
		this.tempid += tempid;
		setTempid(this.tempid);
	}
	
	public void changeTempidWithMin(int tempid,int min){
		this.tempid += tempid;
		this.tempid = this.tempid < min ? min : this.tempid;
		setTempid(this.tempid);
	}
	
	public void changeTempidWithMax(int tempid,int max){
		this.tempid += tempid;
		this.tempid = this.tempid > max ? max : this.tempid;
		setTempid(this.tempid);
	}
	
	public void changeTempidWithMaxMin(int tempid,int max,int min){
		this.tempid += tempid;
		this.tempid = this.tempid < min ? min : this.tempid;
		this.tempid = this.tempid > max ? max : this.tempid;
		setTempid(this.tempid);
	}	
	public String getMc() {
		return mc;
	}
	
	public void setMc(String mc) {
		this.mc = mc;
		addUpdateColumn("名称",mc);
	}
	
	public int getCjrid() {
		return cjrid;
	}
	
	public void setCjrid(int cjrid) {
		this.cjrid = cjrid;
		addUpdateColumn("创建人id",cjrid);
	}
	
	public void changeCjridWith(int cjrid){
		this.cjrid += cjrid;
		setCjrid(this.cjrid);
	}
	
	public void changeCjridWithMin(int cjrid,int min){
		this.cjrid += cjrid;
		this.cjrid = this.cjrid < min ? min : this.cjrid;
		setCjrid(this.cjrid);
	}
	
	public void changeCjridWithMax(int cjrid,int max){
		this.cjrid += cjrid;
		this.cjrid = this.cjrid > max ? max : this.cjrid;
		setCjrid(this.cjrid);
	}
	
	public void changeCjridWithMaxMin(int cjrid,int max,int min){
		this.cjrid += cjrid;
		this.cjrid = this.cjrid < min ? min : this.cjrid;
		this.cjrid = this.cjrid > max ? max : this.cjrid;
		setCjrid(this.cjrid);
	}	
	public int getFwq_id() {
		return fwq_id;
	}
	
	public void setFwq_id(int fwq_id) {
		this.fwq_id = fwq_id;
		addUpdateColumn("服务器_id",fwq_id);
	}
	
	public void changeFwq_idWith(int fwq_id){
		this.fwq_id += fwq_id;
		setFwq_id(this.fwq_id);
	}
	
	public void changeFwq_idWithMin(int fwq_id,int min){
		this.fwq_id += fwq_id;
		this.fwq_id = this.fwq_id < min ? min : this.fwq_id;
		setFwq_id(this.fwq_id);
	}
	
	public void changeFwq_idWithMax(int fwq_id,int max){
		this.fwq_id += fwq_id;
		this.fwq_id = this.fwq_id > max ? max : this.fwq_id;
		setFwq_id(this.fwq_id);
	}
	
	public void changeFwq_idWithMaxMin(int fwq_id,int max,int min){
		this.fwq_id += fwq_id;
		this.fwq_id = this.fwq_id < min ? min : this.fwq_id;
		this.fwq_id = this.fwq_id > max ? max : this.fwq_id;
		setFwq_id(this.fwq_id);
	}	
	public int getZdl() {
		return zdl;
	}
	
	public void setZdl(int zdl) {
		this.zdl = zdl;
		addUpdateColumn("战斗力",zdl);
	}
	
	public void changeZdlWith(int zdl){
		this.zdl += zdl;
		setZdl(this.zdl);
	}
	
	public void changeZdlWithMin(int zdl,int min){
		this.zdl += zdl;
		this.zdl = this.zdl < min ? min : this.zdl;
		setZdl(this.zdl);
	}
	
	public void changeZdlWithMax(int zdl,int max){
		this.zdl += zdl;
		this.zdl = this.zdl > max ? max : this.zdl;
		setZdl(this.zdl);
	}
	
	public void changeZdlWithMaxMin(int zdl,int max,int min){
		this.zdl += zdl;
		this.zdl = this.zdl < min ? min : this.zdl;
		this.zdl = this.zdl > max ? max : this.zdl;
		setZdl(this.zdl);
	}	
	public String getTb() {
		return tb;
	}
	
	public void setTb(String tb) {
		this.tb = tb;
		addUpdateColumn("图标",tb);
	}
	
	public java.util.Date getCjsj() {
		return cjsj;
	}
	
	public void setCjsj(java.util.Date cjsj) {
		this.cjsj = cjsj;
		addUpdateColumn("创建时间",cjsj);
	}
	
	public int getRs() {
		return rs;
	}
	
	public void setRs(int rs) {
		this.rs = rs;
		addUpdateColumn("人数",rs);
	}
	
	public void changeRsWith(int rs){
		this.rs += rs;
		setRs(this.rs);
	}
	
	public void changeRsWithMin(int rs,int min){
		this.rs += rs;
		this.rs = this.rs < min ? min : this.rs;
		setRs(this.rs);
	}
	
	public void changeRsWithMax(int rs,int max){
		this.rs += rs;
		this.rs = this.rs > max ? max : this.rs;
		setRs(this.rs);
	}
	
	public void changeRsWithMaxMin(int rs,int max,int min){
		this.rs += rs;
		this.rs = this.rs < min ? min : this.rs;
		this.rs = this.rs > max ? max : this.rs;
		setRs(this.rs);
	}	
	
	//id
	public List<Yh> getYhsFkLm_id(){
		return YhCache.getByLm_id(id);
	}
	
	//id
	public List<Lt> getLtsFkLm_id(){
		return LtCache.getByLm_id(id);
	}
	
	//id
	public List<Lmsj> getLmsjsFkLm_id(){
		return LmsjCache.getByLm_id(id);
	}
	
	//创建人id
	public Yh getYhPkCjrid(){
		return YhCache.getById(cjrid);
	}
	//服务器_id
	public Fwq getFwqPkFwq_id(){
		return FwqCache.getById(fwq_id);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("tempid", this.tempid);
        result.put("mc", this.mc);
        result.put("cjrid", this.cjrid);
        result.put("fwq_id", this.fwq_id);
        result.put("zdl", this.zdl);
        result.put("tb", this.tb);
        result.put("cjsj", this.cjsj);
        result.put("rs", this.rs);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("tempid", this.tempid);
        result.put("名称", this.mc);
        result.put("创建人id", this.cjrid);
        result.put("服务器_id", this.fwq_id);
        result.put("战斗力", this.zdl);
        result.put("图标", this.tb);
        result.put("创建时间", this.cjsj);
        result.put("人数", this.rs);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.tempid);
		    SK_OutputStream.writeString(out,this.mc);
		    SK_OutputStream.writeInt(out,this.cjrid);
		    SK_OutputStream.writeInt(out,this.fwq_id);
		    SK_OutputStream.writeInt(out,this.zdl);
		    SK_OutputStream.writeString(out,this.tb);
		    SK_OutputStream.writeInt(out,this.rs);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Lm createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Lm lm = new Lm();
		    lm.id = SK_InputStream.readInt(in,null);
		    lm.tempid = SK_InputStream.readInt(in,null);
		    lm.mc = SK_InputStream.readString(in,null);
		    lm.cjrid = SK_InputStream.readInt(in,null);
		    lm.fwq_id = SK_InputStream.readInt(in,null);
		    lm.zdl = SK_InputStream.readInt(in,null);
		    lm.tb = SK_InputStream.readString(in,null);
		    lm.rs = SK_InputStream.readInt(in,null);
		    return lm;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Lm> createForColumnNameList(List<Map<String, Object>> list){
    	List<Lm> lms = new ArrayList<Lm>();
		for (Map<String, Object> map : list) {
			lms.add(createForColumnNameMap(map));
		}
		return lms;
    }
    
    /**
     * 根据map创建对象
     */
    public static Lm createForColumnNameMap(Map<String, Object> map){
    	Lm obj = new Lm();
	    obj.id = SK_Map.getInt("id", map);
	    obj.tempid = SK_Map.getInt("tempid", map);
	    obj.mc = SK_Map.getString("名称", map);
	    obj.cjrid = SK_Map.getInt("创建人id", map);
	    obj.fwq_id = SK_Map.getInt("服务器_id", map);
	    obj.zdl = SK_Map.getInt("战斗力", map);
	    obj.tb = SK_Map.getString("图标", map);
	    obj.rs = SK_Map.getInt("人数", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Lm> createForList(List<Map<String, Object>> list){
    	List<Lm> lms = new ArrayList<Lm>();
		for (Map<String, Object> map : list) {
			lms.add(createForColumnNameMap(map));
		}
		return lms;
    }
    
    /**
     * 根据map创建对象
     */
    public static Lm createForMap(Map<String, Object> map){
    	Lm obj = new Lm();
	    obj.id = SK_Map.getInt("id", map);
	    obj.tempid = SK_Map.getInt("tempid", map);
	    obj.mc = SK_Map.getString("mc", map);
	    obj.cjrid = SK_Map.getInt("cjrid", map);
	    obj.fwq_id = SK_Map.getInt("fwq_id", map);
	    obj.zdl = SK_Map.getInt("zdl", map);
	    obj.tb = SK_Map.getString("tb", map);
	    obj.rs = SK_Map.getInt("rs", map);
        return obj;
    }
    
    public static Lm createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Lm> createForJson(List<String> jsons){
    	List<Lm> lms = new ArrayList<Lm>();
    	for(String json : jsons){
    		lms.add(createForJson(json));
    	}
    	return lms;
    }
    
    /** 延迟插入数据库 */
    public Lm insert(){
    	return LmCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Lm update(){
    	return LmCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return LmCache.delete(this);
    }
    /** 即时插入数据库 */
    public Lm insertAndFlush(){
    	return LmCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Lm updateAndFlush(){
    	return LmCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return LmCache.deleteAndFlush(this);
    }
}